package com.xcity.game.item.operation;

import com.xcity.db.entity.Player;
import com.xcity.game.common.AbstractConfirmOperation;
import com.xcity.game.common.Cause;
import com.xcity.game.event.GameEvents;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemException;
import com.xcity.pomelo.push.msg.TipPush;

import naga.x.App;

public class UseItemOperation extends AbstractConfirmOperation {

	Player target;
	long itemInstanceId;
	int itemCount;
	
	public UseItemOperation(Player ref, Player target, long itemInstanceId, int itemCount, String msg) {
		super(ref, msg);
		this.target = target;
		this.itemInstanceId = itemInstanceId;
		this.itemCount = itemCount;
	}

	@Override
	public void ok(Player player) {
		try {
			GameItem item = player.getBag().useItem(target, itemInstanceId, itemCount, Cause.USE_ITEM);
			if (item != null) {
				App.getApp().getEventManager().addEvent(GameEvents.EVENT_USE_ITEM, player, item.getTemplate(), itemCount, target);
			}
		} catch (ItemException e) {
			player.send(new TipPush(e.getMessage()));
		}
	}
	
}
